import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/Home'
import Dashboard from '@/views/Dashboard'

import BookList from '@/views/book/list'
import BookCategoryList from '@/views/bookcategory/list'

import CanvasList1 from '@/views/canvas/list1'
import CanvasList2 from '@/views/canvas/list2'
import CanvasList3 from '@/views/canvas/list3'

import UserList from '@/views/user/list'
import UserChangePwd from '@/views/user/changepwd'
import UserProfile from '@/views/user/profile'

import API from '@/views/apimanager/index'

// 懒加载方式，当路由被访问的时候才加载对应组件
const Login = resolve => require(['@/views/Login'], resolve)

Vue.use(Router)

let router = new Router({
// mode: 'history',
  routes: [
    {
      path: '/login',
      name: '登录',
      component: Login
    },
    {
      path: '/',
      name: 'home',
      component: Home,
      redirect: '/dashboard',
      leaf: true, // 只有一个节点
      menuShow: true,
      iconCls: 'iconfont icon-home', // 图标样式class
      children: [
        {path: '/dashboard', component: Dashboard, name: '首页', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '用户管理',
      menuShow: true,
      leaf: true, // 只有一个节点
      iconCls: 'iconfont icon-users', // 图标样式class
      children: [
        {path: '/user/list', component: UserList, name: '用户列表', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: 'API管理',
      menuShow: true,
      iconCls: 'iconfont icon-users', // 图标样式class
      children: [
        {path: '/apimanager/index', component: API, name: 'CDN-API认证', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '图书管理',
      menuShow: true,
      iconCls: 'iconfont icon-books',
      children: [
        {path: '/book/list', component: BookList, name: '图书列表', menuShow: true},
        {path: '/book/category', component: BookCategoryList, name: '图书分类', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: 'canvas学习',
      menuShow: true,
      iconCls: 'iconfont icon-books',
      children: [
        {path: '/canvas/list1', component: CanvasList1, name: '基本绘制', menuShow: true},
        {path: '/canvas/list2', component: CanvasList2, name: '简单动画', menuShow: true},
        {path: '/canvas/list3', component: CanvasList3, name: '贪吃蛇', menuShow: true}
      ]
    },
    {
      path: '/',
      component: Home,
      name: '设置',
      menuShow: true,
      iconCls: 'iconfont icon-setting1',
      children: [
        {path: '/user/profile', component: UserProfile, name: '个人信息', menuShow: true},
        {path: '/user/changepwd', component: UserChangePwd, name: '修改密码', menuShow: true}
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log('to:' + to.path)
  if (to.path.startsWith('/login')) {
    window.sessionStorage.removeItem('access-user')
    next()
  } else {
    let user = JSON.parse(window.sessionStorage.getItem('access-user'))
    if (!user) {
      next({path: '/login'})
    } else {
      next()
    }
  }
})

export default router
